/*
 *
 *  ------------------------------------------------------------------
 *  Copyright © 2017 Hangzhou DtDream Technology Co.,Lt d. All rights reserved.
 *  ------------------------------------------------------------------
 *        Product:  EMR
 *    Module Name: NEZHA
 *   Date Created: 17-10-23 下午4:22
 *    Description:
 *  ------------------------------------------------------------------
 *  Modification History
 *  DATE            Name           Description
 *  ------------------------------------------------------------------
 *  2017-10-23      NEZHA EMR
 *  ------------------------------------------------------------------
 * /
 */

package org.xukai.remoting.sdk.web.api;

import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * Created by moriarty on 17-2-23.
 */
public class RestRequestBuilder {

    private URL root;
    private Map<String, String> headers;
    private RestAPI restAPI;
    private Object[] args;
    private RequestBody body;

    public RestRequestBuilder setRootURL(URL root) {
        this.root = root;
        return this;
    }

    public RestRequestBuilder setHeaders(Map<String, String> headers) {
        this.headers = headers;
        return this;
    }

    public RestRequestBuilder setRestAPI(RestAPI restAPI) {
        this.restAPI = restAPI;
        return this;
    }

    public RestRequestBuilder setBody(RequestBody body) {
        this.body = body;
        return this;
    }

    public RestRequestBuilder setArgs(Object... args) {
        this.args = args;
        return this;
    }

    public Request build() throws MalformedURLException {
        Request.Builder builder = new Request.Builder();
        builder.method(restAPI.method, body).headers(Headers.of(headers))
                .url(new URL(root, restAPI.toAPIPath(args)));
        return builder.build();
    }
}
